package com.whc.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.whc.dto.UserLoginDto;
import com.whc.dto.UserRegisterDto;
import com.whc.entity.Goods;
import com.whc.entity.User;
import com.whc.exception.LoginFailedException;
import com.whc.exception.RegisterFailedException;
import com.whc.mapper.UserMapper;
import com.whc.service.UserService;
import com.whc.vo.FriendVo;
import com.whc.vo.UserPageVo;
import com.whc.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.security.auth.login.LoginException;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 韦红材
 * @ClassName UserServiceImpl
 * @data 2024/09/15 16:18
 * @Version V1.0
 **/
@Service
public class UserServiceImpl implements UserService {
    final String userOnline="userOnline";
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    private PasswordEncoder passwordEncoder;

    public User Login(UserLoginDto dto) {
        LambdaQueryWrapper<User> lw = new LambdaQueryWrapper<>();
        lw.eq(User::getUsername,dto.getUsername());
        //无需在验证密码
//        String password=passwordEncoder.encode(dto.getPassword());
        User user = userMapper.selectOne(lw);

//        //原始密码加密
//        password=DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
//        if(user==null||!password.equals(user.getPassword())){
//            throw new LoginFailedException("账号或密码错误");
//        }
        //TODO 这里可以考虑添加一个状态字段，来账号封锁
//        User user = userMapper.selectById(1);
        return user;
    }

    public void addUserMoney(long userid,double money){
        userMapper.addUserMoney(userid,money);
    }

    @Override
    public UserVo getUserVoById(Long userId) {
        User user = userMapper.selectById(userId);
        UserVo userPageVo = new UserVo();
        BeanUtils.copyProperties(user,userPageVo);
        return userPageVo;
    }
    @Override
    public UserPageVo getUserById(long userId) {
        User user = userMapper.selectById(userId);
        UserPageVo userPageVo = new UserPageVo();
        BeanUtils.copyProperties(user,userPageVo);
        return userPageVo;
    }

    public void Register(UserRegisterDto userRegisterDto){
        String username = userRegisterDto.getUsername();
        LambdaQueryWrapper<User> lw = new LambdaQueryWrapper<User>();
        lw.eq(User::getUsername,username);
        User user1 = userMapper.selectOne(lw);
        if(user1!=null){
            throw new RegisterFailedException("用户已存在");
        }
        String password=userRegisterDto.getPassword();
        User user = new User();
        BeanUtils.copyProperties(userRegisterDto,user);

        //将密码加密
//        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8)));
        String encode = passwordEncoder.encode(password);
        user.setPassword(encode);
        user.setCreateTime(LocalDateTime.now());
        user.setMoney(0);
        user.setType(0);
        userMapper.insert(user);
    }

    @Override
    public User getUserByName(String userName) {
        LambdaQueryWrapper<User> lw=new LambdaQueryWrapper<User>();
        lw.eq(User::getUsername,userName);
        User user = userMapper.selectOne(lw);
        return user;
    }



    @Override
    public List<FriendVo> getAllFriends(Long pid) {
        List<FriendVo> friends = userMapper.getAllFriends(pid);
        for(FriendVo f:friends){
            f.setOnline(redisTemplate.hasKey(userOnline+f.getId())?1:0);
        }
        return friends;
    }

    @Override
    public FriendVo getFriendById(Long userId) {
        FriendVo friendVo = userMapper.getFriendById(userId);
        friendVo.setOnline(redisTemplate.hasKey(userOnline+friendVo.getId())?1:0);
        return friendVo;
    }

    @Override
    public List<Integer> getAllUserId() {
        List<Integer> ids = userMapper.getAllUserId();
        return ids;
    }
}
